package cn.sheep.dolphin.report

import cn.sheep.dolphin.common.DolphinAppComm
import cn.sheep.dolphin.config.DolphinConfig
import org.apache.spark.sql.{SQLContext, SaveMode}

/**
  * 省市数据分析
  * author: old sheep
  * QQ: 64341393 
  * Created 2018/11/28
  */
object ProvinceCityAnalysis {

	def main(args: Array[String]): Unit = {

		if(args.size != 2) {
			println(
				"""
				  |Usage: cn.sheep.dolphin.report.ProvinceCityAnalysis
				  |参数：<parquetPath> <outputPath>
				""".stripMargin)
		}

		val Array(parquetPath, outputPath) = args


		val sc = DolphinAppComm.createSparkContext("省市数据分析SQL")
		val sqlc = new SQLContext(sc)

		// 读取parquet文件
		val dataFrame = sqlc.read.parquet(parquetPath)

		// 将dataFrame注册成表
		dataFrame.registerTempTable("t_adlog")

		// 针对数据表进行sql查询: 每个省市的数据记录条数
		val result =sqlc.sql(
			"""
			  |select substring(requestdate, 0, 10) date, provincename, cityname, count(1) cnt
			  |from t_adlog
			  |group by provincename,cityname, substring(requestdate, 0, 10)
			""".stripMargin)


		// JSON
		/*result.coalesce(4).write.json(outputPath)*/

		// MYSQL
		result.write.mode(SaveMode.Append).jdbc(DolphinConfig._url, "r_procity_analysis", DolphinConfig.props)


		sc.stop()

	}

}
